package com.maikang.exammall.repository;

import com.maikang.exammall.entity.Region;
import com.maikang.exammall.entity.dto.IdNameOnlyDTO;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;


@Repository
public interface RegionRepository extends BaseRepository<Region, Long>, JpaSpecificationExecutor<Region> {
    List<Region> findByParentId(Long parentId);

    @Query("SELECT region.id AS id, region.name AS name FROM Region region ") // must use an alias
    Collection<IdNameOnlyDTO> findAllIdAndName();

    @Modifying
    @Query("UPDATE Region region SET region.centerLongitude = :longitude, region.centerLatitude = :latitude WHERE region.id = :id")
    void updateLongLatById(@Param("longitude") Double longitude, @Param("latitude") Double latitude, @Param("id") Long id);

    List<Region> findByLevel(Integer level);
}
